package com.huifaapp.mall.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.Search_showActivity;
import com.huifaapp.mall.activity.ShopCartActivity;
import com.huifaapp.mall.activity.ShopYouhuiShareActivity;
import com.huifaapp.mall.activity.TmallhomepageFragment;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.bean.SearchBean;
import com.huifaapp.mall.uitils.Contig;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by duanyu on 2018/2/1.
 */

public class Seearch_showAdapter extends XRecyclerView.Adapter<Seearch_showAdapter.MyViewHolder>implements View.OnClickListener {
    private List<SearchBean.DataBean.ListBean>list;
    private  Context context;
    private List<Boolean> isChecked;    //每个条目是否为选中
    private List<String> numbers;       //每个条目应显示的数字
    private List<Integer> checkedIndex;   //保存选中的下标
    private List<CheckBox> checkBox;      //保存选中的checkBox
    private int maxChecked = 9;       //最多选中几个
    private int checkedNumber = 0;    //选中的个数
    private int length = 500;  //条目的总长

    public Seearch_showAdapter(List<SearchBean.DataBean.ListBean> list, Context context) {
        this.list = list;
        this.context = context;
        isChecked = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            isChecked.add(false);
        }
        numbers = new ArrayList<>();
        for (int i = 0; i < length; i++) {
            numbers.add("");
        }
        checkedIndex = new ArrayList<>();
        checkBox = new ArrayList<>();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View recyclerViewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.itme_tmall_choiceness, parent, false);
        recyclerViewItem.setOnClickListener(this);
        return new MyViewHolder(recyclerViewItem);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.itme_tmall_text_name.setText("\t\t\t\t"+list.get(position).getGoodsName());
        Glide.with(context).load(list.get(position).getMainImageUrl()).into(holder.itme_tmall_imge);
        holder.itme_tmall_price.setText("¥"+list.get(position).getPrice()+"");
        holder.itme_tmall_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG);
        holder. itme_tmall_discountprice.setText(list.get(position).getConPrice()+"");
        holder.itme_tmall_totalSales.setText(list.get(position).getTotalSales());
        holder.itme_tmall_couponMoney.setText("降"+list.get(position).getCouponMoney()+"元");
        if( list.get(position).getWlCommission()==0|| list.get(position).getWlCommission()==0.0|| list.get(position).getWlCommission()==0.00){
            holder.tv_tmall_lijifenxiang.setText("立即分享" );
            holder.tv_tmall_lijifenxiang_no.setText("立即分享" );
        }else {
            holder.tv_tmall_lijifenxiang.setText("分享赚 ¥" + list.get(position).getWlCommission());
            holder.tv_tmall_lijifenxiang_no.setText("分享赚 ¥" + list.get(position).getWlCommission());
        }
        if (SPUtils.getInstance().getString(SPConstants.KEY_ISSTORE).equals("1")){
            holder.itme_tmall_checkBox.setVisibility(View.VISIBLE);
            holder.tv_tmall_lijifenxiang_pt.setVisibility(View.GONE);
            holder.tv_tmall_lijifenxiang.setVisibility(View.VISIBLE);
        }else{
            holder.itme_tmall_checkBox.setVisibility(View.GONE);
            holder.tv_tmall_lijifenxiang_pt.setVisibility(View.VISIBLE);
            holder.tv_tmall_lijifenxiang.setVisibility(View.GONE);
        }
        if(list.get(position).getIsHot().equals("1")){
            holder.itme_tmall_imge_choiceness.setImageResource(R.drawable.img_blun_biaoqian_baop_1);
        }else{
            holder.itme_tmall_imge_choiceness.setImageResource(R.drawable.rhome_img_1);
        }
        holder.itemView.setTag(position);
        if(list.get(position).getSpuPlatform().equals("jd")){
            holder.iv_tmall_jdOryh.setImageResource(R.drawable.jingdong);
        }else{
            holder.iv_tmall_jdOryh.setImageResource(R.drawable.youhui);
        }
        Log.e("position",position+"");
        holder.itme_tmall_checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean check;
                if (checkedNumber < maxChecked) {
                    if (!isChecked.get(position)) {
                        checkedIndex.add(position);
                        checkBox.add(holder.itme_tmall_checkBox);
                        checkedNumber++;
                        setText();
                    } else {
                        clearText(position, holder.itme_tmall_checkBox);
                    }
                    check = !isChecked.get(position);
                } else {
                    if (checkedNumber == maxChecked && isChecked.get(position)) {
                        clearText(position, holder.itme_tmall_checkBox);
                    } else {
                        Toast.makeText(context, "最多只能选" + maxChecked + "个", Toast.LENGTH_SHORT).show();
                    }
                    check = false;
                }
                isChecked.set(position, check);
                holder.itme_tmall_checkBox.setChecked(isChecked.get(position));
            }
        });
        final Integer tag = new Integer(position);
        //解决RecyclerView和checkBox问题
        holder.itme_tmall_checkBox.setTag(tag);

            if (Search_showActivity.hmap.containsKey(tag)) {
                holder.itme_tmall_checkBox.setChecked(Search_showActivity.hmap.get(tag));
            } else {
                holder.itme_tmall_checkBox.setChecked(false);//true or false 都可以，看实际需求
            }




        Contig.checkedIndexltmalgrid = checkedIndex;
        holder.itemView.setTag(position);
        holder.tv_tmall_lijifenxiang_no.setVisibility(View.GONE);
        for (int s = 0; s < Contig.ShareListtmll.size(); s++) {
            if (list.get(position).getId().equals(Contig.ShareListtmll.get(s))) {
                holder.tv_tmall_lijifenxiang_no.setVisibility(View.VISIBLE);
                holder.tv_tmall_lijifenxiang.setVisibility(View.GONE);
                Log.e("5647","youbianhua          "+list.get(position).getId());


            }
        }
        holder.tv_tmall_lijifenxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ShopYouhuiShareActivity.class);
                intent.putExtra("id",list.get(position).getId());
                context.startActivity(intent);
                holder.tv_tmall_lijifenxiang_no.setVisibility(View.VISIBLE);
                holder.tv_tmall_lijifenxiang.setVisibility(View.GONE);
            }
        });
    }
    public void clearText(int position, CheckBox cb_checked) {
        for (int i = 0; i < checkedIndex.size(); i++) {
            if (checkedIndex.get(i) == position) {
                numbers.set(position, "");
                checkedIndex.remove(i);
                checkBox.remove(i);
                break;
            }
        }
        checkedNumber--;
        setText();
        numbers.set(position, "");
        cb_checked.setText("");
    }

    public void setText() {
        for (int i = 0; i < checkedIndex.size(); i++) {
            numbers.set(checkedIndex.get(i), (i + 1) + "");
            checkBox.get(i).setText(numbers.get(checkedIndex.get(i)));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (mMyItemclickListener != null) {
            mMyItemclickListener.itemclick(v, (Integer) v.getTag());
        }
    }

    public interface MyItemclickListener {
        void itemclick(View view, int position);


    }
    public MyItemclickListener mMyItemclickListener;

    public void setmMyItemclickListener(MyItemclickListener mMyItemclickListener) {
        this.mMyItemclickListener = mMyItemclickListener;
    }
    class MyViewHolder extends XRecyclerView.ViewHolder {
        private final ImageView itme_tmall_imge;
        private final TextView itme_tmall_text_name;
        private final TextView itme_tmall_price;
        private TextView itme_tmall_discountprice;
        private final TextView itme_tmall_totalSales;
        private final TextView itme_tmall_couponMoney;
        private ImageView itme_tmall_imge_choiceness;
        private  CheckBox itme_tmall_checkBox;
        private TextView tv_tmall_lijifenxiang_no;
        private TextView tv_tmall_lijifenxiang;
        private RelativeLayout rl_shar_item_gv;
        private TextView tv_tmall_lijifenxiang_pt;
        private ImageView iv_tmall_jdOryh;
        public MyViewHolder(View itemView) {
            super(itemView);
            itme_tmall_imge = (ImageView) itemView.findViewById(R.id.itme_tmall_imge);
            itme_tmall_text_name = (TextView) itemView.findViewById(R.id.itme_tmall_text_name);
            itme_tmall_price = (TextView) itemView.findViewById(R.id.itme_tmall_price);
            itme_tmall_discountprice = (TextView) itemView.findViewById(R.id.itme_tmall_discountprice);
            itme_tmall_totalSales = (TextView) itemView.findViewById(R.id.itme_tmall_totalSales);
            itme_tmall_couponMoney = (TextView) itemView.findViewById(R.id.itme_tmall_couponMoney);
            itme_tmall_imge_choiceness= (ImageView) itemView.findViewById(R.id.itme_tmall_imge_choiceness);
            tv_tmall_lijifenxiang= (TextView) itemView.findViewById(R.id.tv_tmall_lijifenxiang);
            tv_tmall_lijifenxiang_no= (TextView) itemView.findViewById(R.id.tv_tmall_lijifenxiang_no);
            itme_tmall_checkBox= (CheckBox) itemView.findViewById(R.id.itme_tmall_checkBox);
            rl_shar_item_gv= (RelativeLayout) itemView.findViewById(R.id.rl_shar_item_gv);
            tv_tmall_lijifenxiang_pt= (TextView) itemView.findViewById(R.id.tv_tmall_lijifenxiang_pt);
            iv_tmall_jdOryh= (ImageView) itemView.findViewById(R.id.iv_tmall_jdOryh);
        }
    }
}
